package com.zf.shuashua.ui.dialog;

import android.app.Dialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.meis.base.mei.base.BaseDialog;
import com.vise.xsnow.http.callback.ACallback;
import com.zf.shuashua.R;
import com.zf.shuashua.api.HttpData;
import com.zf.shuashua.bean.AddBlack;
import com.zf.shuashua.bean.BaseData;
import com.zf.shuashua.utils.ToastUtil;

public class BottomOperateDialog extends BaseDialog implements View.OnClickListener {

    private boolean isOwn;
    private String commentId;
    private int position;
    private String commentContent;
    private boolean isLevel2Reply;

    private TextView tvCopy;
    private View viewLine;
    private TextView tvDelete;
    private TextView tvCancel;
    private TextView tv_ad;
    private TextView tv_pron;
    private TextView tv_saorao;
    private TextView tv_polic;

    private OnOperateListener mOperateListener;

    public BottomOperateDialog(boolean isOwn, boolean level2Reply, String id, int position, String content, OnOperateListener listener) {
        this.isOwn = isOwn;
        this.commentId = id;
        this.position = position;
        this.commentContent = content;
        this.mOperateListener = listener;
        this.isLevel2Reply = level2Reply;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new BottomSheetDialog(getContext(), R.style.BottomCommentOperateStyle);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_bottom_comment_operate;
    }

    @Override
    protected void initView() {
        tvCopy = findViewById(R.id.tv_copy);
        viewLine = findViewById(R.id.view_line);
        tvDelete = findViewById(R.id.tv_delete);
        tvCancel = findViewById(R.id.tv_cancel);

        tv_ad = findViewById(R.id.tv_ad);
        tv_ad.setOnClickListener(this);

        tv_pron = findViewById(R.id.tv_pron);
        tv_pron.setOnClickListener(this);

        tv_saorao = findViewById(R.id.tv_saorao);
        tv_saorao.setOnClickListener(this);

        tv_polic = findViewById(R.id.tv_polic);
        tv_polic.setOnClickListener(this);

        if (isOwn) {
            viewLine.setVisibility(View.VISIBLE);
            tvDelete.setVisibility(View.VISIBLE);
        }

        tvCopy.setOnClickListener(v -> {
            toCopy(getContext(), commentContent);
            ToastUtil.show(getContext(), "复制成功");
            dismiss();
        });

        tvDelete.setOnClickListener(v -> {
            HttpData.getInstance().deleteVideoComment(isLevel2Reply, commentId, new ACallback<BaseData<String>>() {
                @Override
                public void onSuccess(BaseData<String> data) {
                    if (data.getCode() == 200) {
                        mOperateListener.onDelete(v, position);
                        ToastUtil.show(getContext(), "删除成功");
                    }
                    dismiss();
                }

                @Override
                public void onFail(int errCode, String errMsg) {
                    ToastUtil.show(getContext(), errMsg);
                    dismiss();
                }
            });
        });

        tvCancel.setOnClickListener(v -> {
            dismiss();
        });
    }

    @Override
    protected void initData() {

    }

    private void toCopy(Context context, String text) {
        ClipboardManager mClipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        mClipboardManager.setPrimaryClip(ClipData.newPlainText(null, text));
    }

    @Override
    public void onClick(View v) {
        int tip_off_id = 1;
        switch (v.getId()){
            case R.id.tv_ad:
                tip_off_id = 1;
                break;
            case R.id.tv_pron:
                tip_off_id = 2;
                break;
            case R.id.tv_saorao:
                tip_off_id = 3;
                break;
            case R.id.tv_polic:
                tip_off_id = 4;
                break;
        }
        HttpData.getInstance().addReport(tip_off_id+"", isLevel2Reply?"reply":"comment", commentId, new ACallback<AddBlack>() {
            @Override
            public void onSuccess(AddBlack data) {
                if (data.getCode() == 200) {
                    ToastUtil.show(getContext(), "举报成功");
                }
                dismiss();
            }

            @Override
            public void onFail(int errCode, String errMsg) {
                dismiss();
            }
        });


    }

    public interface OnOperateListener {
        void onDelete(View v, int position);
    }
}
